﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

namespace IronSuit.Utility
{
	public static class JSONHelper
	{
		public static  string ToJson<T>(T t)
		{
			DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
			MemoryStream ms = new MemoryStream();
			ds.WriteObject(ms, t);

			string strReturn = Encoding.UTF8.GetString(ms.ToArray());
			ms.Close();
			return strReturn;
		}

		public static T FromJson<T>(string strJson) where T : class
		{
			DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
			MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));

			return ds.ReadObject(ms) as T;
		}
	}
}
